//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.model.wills.ContractInfo;
import com.aidong.model.wills.ContractSign;
import com.aidong.model.wills.PtBespeak;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.stereotype.Repository;

@Repository
public class PtBespeakDao extends BaseDao<PtBespeak, Integer> {
    public PtBespeakDao() {
    }

    public List<PtBespeak> getListByCoachDay(String coachCode, Date date1, Date date2) {
        String sql = "select b.autoId,b.shopCode,b.contractCode,b.memberCode, bespeakDate,\ndatepart(hh, classTime) classHour, ptCode, coachCode, classTime, b.status, m.chName memberName\nfrom PtBespeak b inner join MemberInfo m on b.memberCode=m.memberCode\nwhere coachCode=:p1 and classTime>=:p2 and classTime<:p3 and b.status>0\norder by classTime";
        return this.queryListBySql(PtBespeak.class, sql, new Object[]{coachCode, date1, date2});
    }

    public List<PtBespeak> getListByCoachDay(String coachCode, String memberCode, Date date1, Date date2) {
        String sql = "select b.autoId,b.shopCode,b.contractCode,b.memberCode, bespeakDate,\ndatepart(hh, classTime) classHour, ptCode, coachCode, classTime, b.status, m.chName memberName\nfrom PtBespeak b inner join MemberInfo m on b.memberCode=m.memberCode\nwhere coachCode=:p1 and b.memberCode=:p2 and classTime>=:p3 and classTime<:p4 and b.status>0\norder by classTime";
        return this.queryListBySql(PtBespeak.class, sql, new Object[]{coachCode, memberCode, date1, date2});
    }

    public List<PtBespeak> getPrintListByMemberCode(String shopCode, String memberCode, Date date1, Date date2) {
        String sql = "select * from PtBespeak where shopCode=:p1 and memberCode=:p2 and classTime>=:p3 and classTime<:p4\n and status=2 and printLabel=0 order by classTime";
        return this.queryListBySql(PtBespeak.class, sql, new Object[]{shopCode, memberCode, date1, date2});
    }

    public void queryPageList(Pagination<PtBespeak> pg, ConditionMap cd) {
        ArrayList params = new ArrayList();
        String sqlw = cd.toSql(params);
        String sql = "select b.autoId,b.shopCode,b.contractCode,b.memberCode, bespeakDate, bespeakType,\nptCode, coachCode, classTime, b.status, b.printLabel, b.printTime, clearTime, cancelTime, m.chName memberName\nfrom PtBespeak b inner join MemberInfo m on b.memberCode=m.memberCode\n" + (sqlw.length() > 0 ? "where " + sqlw : "") + " order by classTime desc";
        this.queryPageListBySql(PtBespeak.class, pg, sql, params.toArray());
    }

    public void queryPageList2(Pagination<ContractInfo> pg, ConditionMap cd) {
        ArrayList params = new ArrayList();
        String sqlw = cd.toSql(params);
        String sql = "select b.autoId,b.shopCode,b.contractCode,b.memberCode, b.bespeakDate, b.bespeakType, b.ptCode, b.coachCode,\nb.classTime, b.status, b.printLabel, b.printTime, b.clearTime, b.cancelTime, m.chName memberName, m.cardCode,\nc.saleDate, c.beginTime, c.endTime, c.factPrice, c.quantity, c.bespeakQuantity, c.clearQuantity\n,c.SalesCode,c.SalesCode2,b.createUser\nfrom PtBespeak b inner join PtContract c on b.contractCode=c.contractCode\ninner join MemberInfo m on b.memberCode=m.memberCode\n" + (sqlw.length() > 0 ? "where " + sqlw : "") + " order by b.classTime desc";
        this.queryPageListBySql(ContractInfo.class, pg, sql, params.toArray());
    }

    public void queryPageListReview(Pagination<ContractInfo> pg, ConditionMap cd) {
        ArrayList params = new ArrayList();
        String sqlw = cd.toSql(params);
        String sql = "select b.autoId,b.shopCode,b.contractCode,b.memberCode, b.bespeakDate, b.bespeakType, b.ptCode, b.coachCode,\nb.classTime, b.status, b.printLabel, b.printTime, b.clearTime, b.cancelTime, m.chName memberName, m.cardCode,\nc.saleDate, c.beginTime, c.endTime, c.factPrice, c.quantity, c.bespeakQuantity, c.clearQuantity\n,c.SalesCode,c.SalesCode2,b.createUser, s.status signStatus, s.updateTime reviewSubmitDate\nfrom PtBespeak b inner join PtContract c on b.contractCode=c.contractCode\ninner join MemberInfo m on b.memberCode=m.memberCode\ninner join ContractSign s on b.autoId=s.saleId and s.contractType=\'10\'\n" + (sqlw.length() > 0 ? "where " + sqlw : "") + " order by b.classTime desc";
        this.queryPageListBySql(ContractInfo.class, pg, sql, params.toArray());
    }

    public Integer getBespeakCount(String contractCode) {
        String sql = "select count(*) from PtBespeak where contractCode=:p1 and status>0";
        return (Integer) this.getBySql(Integer.class, sql, new Object[]{contractCode});
    }

    public void addBespeak(PtBespeak bespeak) {
        this.add(bespeak);
        String sql = "update PtContract set bespeakQuantity=bespeakQuantity+1, updateTime=getdate() where contractCode=:p1";
        this.updateBySql(sql, new Object[]{bespeak.getContractCode()});
    }

    public boolean addBespeakSign(PtBespeak bespeak) {
        Integer id = (Integer) this.getBySql(Integer.class, "select autoId from ContractSign where contractType=:p1 and saleId=:p2", new Object[]{Integer.valueOf(10), bespeak.getAutoId()});
        if (id == null) {
            String memberName = (String) this.getBySql(String.class, "select chName from MemberInfo where memberCode=:p1", new Object[]{bespeak.getMemberCode()});
            ContractSign sign = new ContractSign();
            sign.setShopCode(bespeak.getShopCode());
            sign.setContractCode(String.valueOf(bespeak.getAutoId()));
            sign.setContractType("10");
            sign.setSaleId(bespeak.getAutoId());
            sign.setMemberCode(bespeak.getMemberCode());
            sign.setMemberName(memberName == null ? "" : memberName);
            sign.setStatus(Integer.valueOf(1));
            sign.setSalesCode(bespeak.getCoachCode());
            sign.setSalesCode2("");
            sign.setCreateUser(bespeak.getCreateUser());
            sign.setCreateTime(new Date());
            sign.setUpdateTime(new Date());
            this.getSession().save(sign);
            return true;
        } else {
            return false;
        }
    }

    public void updateClear(PtBespeak bespeak) {
        String sql = "update PtBespeak set status=1, clearUser=:p1, clearTime=:p2 where autoId=:p3 and status=2";
        if (this.updateBySql(sql, new Object[]{bespeak.getClearUser(), bespeak.getClearTime(), bespeak.getAutoId()}) > 0) {
            sql = "update PtContract set clearQuantity=clearQuantity+1, lastClearTime=getdate(), updateTime=getdate() where contractCode=:p1";
            this.updateBySql(sql, new Object[]{bespeak.getContractCode()});
        }

    }

    public void updateClear(List<PtBespeak> list) {
        Iterator i$ = list.iterator();

        while (i$.hasNext()) {
            PtBespeak bespeak = (PtBespeak) i$.next();
            String sql = "update PtBespeak set status=1, clearUser=:p1, clearTime=:p2 where autoId=:p3 and status in (2,3)";
            if (this.updateBySql(sql, new Object[]{bespeak.getClearUser(), bespeak.getClearTime(), bespeak.getAutoId()}) > 0) {
                sql = "update PtContract set clearQuantity=clearQuantity+1, lastClearTime=getdate(), updateTime=getdate() where contractCode=:p1";
                this.updateBySql(sql, new Object[]{bespeak.getContractCode()});
            }
        }

    }

    public void updateCancel(PtBespeak bespeak) {
        String sql = "update PtBespeak set status=0, canceType=:p1, cancelUser=:p2, cancelTime=:p3 where autoId=:p4 and status=2";
        if (this.updateBySql(sql, new Object[]{bespeak.getCanceType(), bespeak.getCancelUser(), bespeak.getCancelTime(), bespeak.getAutoId()}) > 0) {
            sql = "update PtContract set bespeakQuantity=bespeakQuantity-1, updateTime=getdate() where contractCode=:p1";
            this.updateBySql(sql, new Object[]{bespeak.getContractCode()});
        }

    }

    public void updatePrint(PtBespeak bespeak) {
        String sql = "update PtBespeak set printLabel=:p1, printTime=:p2, printUser=:p3 where autoId=:p4";
        this.updateBySql(sql, new Object[]{bespeak.getPrintLabel(), bespeak.getPrintTime(), bespeak.getPrintUser(), bespeak.getAutoId()});
    }

    public void updateAutoCancel() {
        String sql = "select pb.autoId, pb.contractCode, cs.autoId status\nfrom PtBespeak pb left join ContractSign cs on pb.autoId=cs.saleId and cs.contractType=10\nwhere pb.status=2 and printLabel=0 and (cs.status is null or cs.status=1)\n and classTime>=\'2016-07-01\' and classTime<convert(datetime, convert(varchar(10),getdate()))\norder by classTime";
        List list = this.queryListBySql(PtBespeak.class, sql, new Object[0]);
        Iterator i$ = list.iterator();

        while (i$.hasNext()) {
            PtBespeak bespeak = (PtBespeak) i$.next();
            sql = "update PtBespeak set status=0, canceType=:p1, cancelUser=:p2, cancelTime=:p3 where autoId=:p4 and status=2";
            if (this.updateBySql(sql, new Object[]{"1", "system", new Date(), bespeak.getAutoId()}) > 0) {
                sql = "update PtContract set bespeakQuantity=bespeakQuantity-1, updateTime=getdate() where contractCode=:p1";
                this.updateBySql(sql, new Object[]{bespeak.getContractCode()});
            }

            if (bespeak.getStatus() != null) {
                sql = "delete from ContractSign where autoId=:p1";
                this.updateBySql(sql, new Object[]{bespeak.getStatus()});
            }
        }

    }
}
